package OSMS.action.goodsfunc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import OSMS.dao.GoodsDao;
import OSMS.entity.Car;
import OSMS.entity.Customers;
import OSMS.entity.Goods;
/*
 * 加入购物车页面
 * 	业务逻辑如下：
 * 	1、通过request.getParameter("cid")首先从商品详情页面（detailPage.jsp）中获取用户编号cid并转化为int型
 * 	2、通过调用GoodsDao中的getCarById（cid）：来得到一个装有购物车中所有数据的列表carList
 * 	3、将该列表装到域对象request中
 * 	4、请求转发到showCar.jsp页面显示购物车详细信息
 */

public class inCarServlet extends HttpServlet{
	private GoodsDao dao = new GoodsDao();

	protected void doGet(
			HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		//此处获取的cid就是detailPage.jsp中获取的全局变量：用户编码
		
		HttpSession session = request.getSession();
		//取session中的gid
		Object obj_g = session.getAttribute("gList");
		ArrayList<Goods> gList = (ArrayList<Goods>)obj_g;		
		int gid = gList.get(0).getGid();
		int gstore = gList.get(0).getGstore();
		if(gstore==0)
		{
			request.getRequestDispatcher("../goods/fail_to_update_goods_num.jsp").forward(request, response);
			return;
		}
			
		System.out.println("gid是"+Integer.toString(gid));
		
		//取session中的cid
		Object obj_c = session.getAttribute("cus");
		Customers cus = (Customers)obj_c;
		int cid = cus.getCid();
		System.out.println("cid是"+Integer.toString(cid));
		//判断该用户是否已添加该商品进购物车
		boolean flag = false;
		try {
			flag = dao.checkCar(cid, gid);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if(flag==true)
		{
			boolean flag_update = false;
			System.out.println(flag);
			try {
				flag = 	dao.updateCar(cid, gid);
				List<Car> carList = dao.getCarById(cid);
				session.setAttribute("carList",carList);
//				request.getRequestDispatcher("showCar.jsp").forward(request, response);
//				response.sendRedirect(request.getServletContext().getContextPath() + "/jsp/goods/showCar.jsp");
//				return;
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		else
		{
			int good_num = 1;
			float gprice = gList.get(0).getGprice();
			boolean flag_add = false ; 
			String picture = gList.get(0).getPicture();
			String gname = gList.get(0).getGname();
			request.setCharacterEncoding("UTF-8");
			//根据用户编号查询该用户购物车信息信息
			try {
				//调用新增方法向购物车中插入数据
				flag_add = dao.addCar(cid,gid,gprice,good_num,picture,gname);
				System.out.println(flag_add);
				// 1. 调用GoodsDao的getCarById方法通过用户编号查询购物车（Car）中数据
				List<Car> carList = dao.getCarById(cid);
				//测试所用输出
				System.out.println(carList.toString());
				// 2. 将list数据装入域对象中。
				session.setAttribute("carList", carList);
//				request.setAttribute("carList",carList);
				// 3. 选择对应的跳转方式（请求转发）跳转到showCar.jsp购物车显示页面。
//				request.getRequestDispatcher("showCar.jsp").forward(request, response);
//				response.sendRedirect(request.getServletContext().getContextPath() + "/jsp/goods/showCar.jsp");
			}catch(Exception e) {
			e.printStackTrace();
		}
		}
		response.sendRedirect(request.getServletContext().getContextPath() + "/jsp/goods/detailPage.jsp");
		

	}
	@Override
	protected void doPost(
			HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}

